package com.xoa.service.party;


import com.xoa.dao.department.DepartmentMapper;
import com.xoa.dao.party.HighPointMapper;
import com.xoa.model.party.HighPoint;
import com.xoa.model.users.Users;
import com.xoa.util.common.log.FileUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.common.wrapper.BaseWrapper;
import com.xoa.util.page.PageParams;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.poi.hssf.usermodel.*;
//import org.apache.poi.hssf.util.Region;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 程叶同 on 2018/1/6.
 * 空气重污染 高值点位
 */
@Service
public class HIghPointService {
    @Autowired
    HighPointMapper highPointMapper;
    @Autowired
    DepartmentMapper departmentMapper;

    /**
    * @Author 程叶同
    * @Description 添加
    * @Date 11:39 2018/1/6
    */

    public BaseWrapper addHighpoint(HighPoint highPoint){
        BaseWrapper baseWrapper=new BaseWrapper();
        if(highPoint!=null){
            int insert = highPointMapper.insert(highPoint);
            if(insert>0){
                baseWrapper.setMsg("添加成功");
                baseWrapper.setFlag(true);
            }else{
                baseWrapper.setMsg("添加失败");
            }

        }
        return baseWrapper;
    }

/**
* @Author 程叶同
* @Description 删除
* @Date 13:19 2018/1/6
*/

    public BaseWrapper deleteHighpoint(HttpServletRequest request, Integer id){
        BaseWrapper baseWrapper=new BaseWrapper();
        Users users= SessionUtils.getSessionInfo(request.getSession(), Users.class,new Users());
        if (id!=null){
            HighPoint highPoint = highPointMapper.selectByPrimaryKey(id);
            if(highPoint!=null){
                    int num=highPointMapper.deleteByPrimaryKey(id);
                    if(num>0){
                        baseWrapper.setMsg("删除成功");
                        baseWrapper.setFlag(true);
                    }else{
                        baseWrapper.setFlag(false);
                        baseWrapper.setMsg("删除失败");
                    }
            }

        }
        return  baseWrapper;
    }
    /**
    * @Author 程叶同
    * @Description 修改
    * @Date 13:16 2018/1/6
    */
    public BaseWrapper updateHighPoint(HttpServletRequest request, HighPoint highPoint){
        BaseWrapper baseWrapper=new BaseWrapper();
        Users users= SessionUtils.getSessionInfo(request.getSession(), Users.class,new Users());
        if(highPoint!=null){

                int i = highPointMapper.updateByPrimaryKey(highPoint);
                if(i>0){
                   baseWrapper.setMsg("修改成功");
                   baseWrapper.setFlag(true);
                }else{
                    baseWrapper.setFlag(false);
                    baseWrapper.setMsg("修改失败");
                }

            }

        return  baseWrapper;
    }

    /**
    * @Author 程叶同
    * @Description 查询全部信息
    * @Date 13:20 2018/1/6
    */
 public BaseWrapper getHighPointList(HttpServletRequest request, Integer page, Integer pageSize, boolean useFlag, String subcounty){
     Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class,new Users());
     BaseWrapper baseWrapper=new BaseWrapper();
     List<HighPoint> resultlists=new ArrayList<HighPoint>();
     Map<String,Object> map=new HashMap<String,Object>();
     Map<String,Object> map1=new HashMap<String,Object>();
     PageParams pageParams=new PageParams();
     pageParams.setUseFlag(useFlag);
     pageParams.setPage(page);
     pageParams.setPageSize(pageSize);
     map.put("subcounty",subcounty);
     map1.put("subcounty",subcounty);
     map.put("page",page-1);
     map.put("pageParams",pageParams);
     int total=0;
     resultlists=highPointMapper.selectList(map);
     total=highPointMapper.totalItems(map1);
     if(resultlists!=null){
         baseWrapper.setFlag(true);
         baseWrapper.setData(resultlists);
         baseWrapper.setMsg("查询成功");
     }else{
         baseWrapper.setMsg("查询失败");
         baseWrapper.setFlag(false);
     }
     return  baseWrapper;
 }
/**
* @Author 程叶同
* @Description 查询单条记录
* @Date 13:31 2018/1/6
*/
public BaseWrapper getHighPointById(Integer id){
    BaseWrapper baseWrapper=new BaseWrapper();
    if(id!=null){
        HighPoint highPoint = highPointMapper.selectByPrimaryKey(id);
        if(highPoint!=null){
            baseWrapper.setData(highPoint);
            baseWrapper.setMsg("查询成功");
            baseWrapper.setFlag(true);
        }else{
            baseWrapper.setFlag(false);
            baseWrapper.setMsg("查询失败");
        }
    }
    return baseWrapper;
  }


  /**
  * @Author 程叶同
  * @Description 导出
  * @Date 14:51 2018/1/6
  */

  public BaseWrapper fileOut(HttpServletRequest request, HttpServletResponse response, String ids){
      BaseWrapper baseWrapper=new BaseWrapper();
      HSSFWorkbook Workbook=new HSSFWorkbook();
      HSSFCellStyle cellStyle = Workbook.createCellStyle();
      //cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
      HSSFFont font = Workbook.createFont();
      cellStyle.setFont(font);
      font.setFontName("仿宋_GB2312");
      ///font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
      font.setFontHeightInPoints((short)11);
      HSSFSheet sheet=Workbook.createSheet("环保巡查APP高值网格排查整治情况统计表");
      HSSFRow row = sheet.createRow(0);
      HSSFCell cell = row.createCell(2);
      cell.setCellValue(new HSSFRichTextString("环保巡查高值网格排查整治情况统计表"));
      cell.setCellStyle(cellStyle);

      HSSFRow row1 = sheet.createRow(1);
      HSSFCell cell1 = row1.createCell(0);
      cell1.setCellValue(new HSSFRichTextString("属地政府"));
      cell1.setCellStyle(cellStyle);
      HSSFCell cell2 = row1.createCell(5);
      cell2.setCellValue(new HSSFRichTextString("填报日期"));
      cell2.setCellStyle(cellStyle);
      HSSFCell cell3 = row1.createCell(9);
      cell3.setCellValue(new HSSFRichTextString("填报人"));
      cell3.setCellStyle(cellStyle);
      HSSFCell cell4 = row1.createCell(12);
      cell4.setCellValue(new HSSFRichTextString("联系方式"));
      cell4.setCellStyle(cellStyle);

      HSSFRow row2 = sheet.createRow(2);
      HSSFCell cell5 = row2.createCell(0);
      cell5.setCellValue(new HSSFRichTextString("序号"));
      cell5.setCellStyle(cellStyle);
      HSSFCell cell6 = row2.createCell(1);
      cell6.setCellValue(new HSSFRichTextString("高值网络名称(村)"));
      cell6.setCellStyle(cellStyle);
      HSSFCell cell7 = row2.createCell(2);
      cell7.setCellValue(new HSSFRichTextString("是否手机签到"));
      cell7.setCellStyle(cellStyle);
      HSSFCell cell8 = row2.createCell(3);
      cell8.setCellValue(new HSSFRichTextString("污染源情况统计"));
      cell8.setCellStyle(cellStyle);
      HSSFCell cell9 = row2.createCell(11);
      cell9.setCellValue(new HSSFRichTextString("完成整治数(个)"));
      cell9.setCellStyle(cellStyle);
      HSSFCell cell10 = row2.createCell(12);
      cell10.setCellValue(new HSSFRichTextString("未完成整治数(个)"));
      cell10.setCellStyle(cellStyle);
      HSSFCell cell11 = row2.createCell(13);
      cell11.setCellValue(new HSSFRichTextString("备注"));
      cell11.setCellStyle(cellStyle);

      HSSFRow row3 = sheet.createRow(3);
      HSSFCell cell12 = row3.createCell(3);
      cell12.setCellValue(new HSSFRichTextString("居民散煤"));
      cell12.setCellStyle(cellStyle);
      HSSFCell cell13 = row3.createCell(4);
      cell13.setCellValue(new HSSFRichTextString("道路扬尘"));
      cell13.setCellStyle(cellStyle);
      HSSFCell cell14 = row3.createCell(5);
      cell14.setCellValue(new HSSFRichTextString("工地扬尘"));
      cell14.setCellStyle(cellStyle);
      HSSFCell cell15 = row3.createCell(6);
      cell15.setCellValue(new HSSFRichTextString("工业废气"));
      cell15.setCellStyle(cellStyle);
      HSSFCell cell16 = row3.createCell(7);
      cell16.setCellValue(new HSSFRichTextString("餐饮油烟"));
      cell16.setCellStyle(cellStyle);
      HSSFCell cell17 = row3.createCell(8);
      cell17.setCellValue(new HSSFRichTextString("散乱污"));
      cell17.setCellStyle(cellStyle);
      HSSFCell cell18 = row3.createCell(9);
      cell18.setCellValue(new HSSFRichTextString("着火点"));
      cell18.setCellStyle(cellStyle);
      HSSFCell cell19 = row3.createCell(10);
      cell19.setCellValue(new HSSFRichTextString("燃煤锅炉(小茶炉)"));
      cell19.setCellStyle(cellStyle);
  /*    Region region1 = new Region(0, (short)2, 0, (short)12);
      Region region2 = new Region(1, (short)0, 1, (short)2);
      Region region3 = new Region(1, (short)5, 1, (short)8);
      Region region4 = new Region(1, (short)9, 1, (short)11);
      Region region5 = new Region(1, (short)12, 1, (short)13);
      Region region6 = new Region(2, (short)0, 3, (short)0);
      Region region7 = new Region(2, (short)1, 3, (short)1);
      Region region8 = new Region(2, (short)2, 3, (short)2);
      Region region9 = new Region(2, (short)3, 2, (short)10);
      Region region10 = new Region(2, (short)11, 3, (short)11);
      Region region11 = new Region(2, (short)12, 3, (short)12);
      Region region12= new Region(2, (short)13, 3, (short)13);
      sheet.addMergedRegion(region1);
      sheet.addMergedRegion(region2);
      sheet.addMergedRegion(region3);
      sheet.addMergedRegion(region4);
      sheet.addMergedRegion(region5);
      sheet.addMergedRegion(region6);
      sheet.addMergedRegion(region7);
      sheet.addMergedRegion(region8);
      sheet.addMergedRegion(region9);
      sheet.addMergedRegion(region10);
      sheet.addMergedRegion(region11);
      sheet.addMergedRegion(region12);*/
      //查询数据
      List<HighPoint> highPoints=null;
      if(ids!=null&&ids!=""){
          String[] id=ids.split(",");
          Map<String,Object> map=new HashMap<String,Object>();
          map.put("id",id);
          highPoints=highPointMapper.selectList(map);
      }
    try {
          String[] Propertys={"highId","highgridName","phoneCheck","residentsCoal","roadDust","constructionDust","industrialGas","diningLampblack","scatteredDirt","ignition","coalBolier","regulationFinished","regulationUnfinished","remark"};
          if(highPoints!=null){

              for (int j = 0; j < highPoints.size(); j++) {
                  HSSFRow rowData = sheet.createRow(j + 4);
                  HighPoint t = highPoints.get(j);
                  for(int k=0; k<Propertys.length; k++){
                      Object value = PropertyUtils.getSimpleProperty(t, Propertys[k]);
                      // = (String) BeanUtils.getBeanProperty(t, beanPropertys[k].toString());
                      HSSFCell cellData = rowData.createCell(k);
                      try {
                          if (!value.equals("")&&value!=null) {
                              cellData.setCellValue(value.toString());
                          }else {
                              cellData.setCellValue("");
                          }
                      } catch (Exception e) {
                      }
                  }
              }
              for (int i=0;i<highPoints.size();i++){

              }
          }

        ServletOutputStream out = response.getOutputStream();
        String filename = "高值定位统计表.xls";
        filename = FileUtils.encodeDownloadFilename(filename,
                request.getHeader("user-agent"));
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("content-disposition",
                "attachment;filename=" + filename);
        Workbook.write(out);
        out.flush();
        out.close();
    }catch (Exception e){
        e.printStackTrace();
    }
      return baseWrapper;
  }
}
